﻿
namespace WXWorkSvc.Common.Data;
/// <summary>
/// 0:WXWorkSvc,
/// 1:OpenPms
/// </summary>
[EntityTypeConfiguration(typeof(TLoggerConfig))]
public class TLogger : IConvertible
{
    public long Id { get; set; }
    [Column(nameof(AppId))]
    public int AppId { get; set; }
    [Display(Name = "日期")]
    [Column(nameof(Dt))]
    public DateTime Dt { get; set; }
    [Display(Name = "类别")]
    [Column(nameof(Name))]
    public string Name { get; set; } = default!;
    [Display(Name = "等级")]
    [Column(nameof(LogLevel))]
    public LogLevel LogLevel { get; set; } = default!;
    [Display(Name = "内容")]
    [Column(nameof(Msg))]
    public string Msg { get; set; } = default!;
    [Display(Name = "内容")]
    public string ShortMsg { get; set; } = default!;
    //[Display(Name = "异常")]
    //public string Exception { get; set; } = default!;
    public TLogger() { }
    public TLogger(string name, LogLevel level, string? msg, Exception? exception)
    {

        Name = name;
        LogLevel = level;
        Msg = msg ?? "" + "\r\n" + exception?.ToString() ?? "";
        Dt = DateTime.Now;
    }
    public TLogger(int appId, string name, LogLevel level, string? msg, Exception? exception)
    {
        AppId = appId;
        Name = name;
        LogLevel = level;
        Msg = msg ?? "" + "\r\n" + exception?.ToString() ?? "";
        Dt = DateTime.Now;
    }

    TypeCode IConvertible.GetTypeCode()
    {
        throw new NotImplementedException();
    }

    bool IConvertible.ToBoolean(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    byte IConvertible.ToByte(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    char IConvertible.ToChar(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    DateTime IConvertible.ToDateTime(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    decimal IConvertible.ToDecimal(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    double IConvertible.ToDouble(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    short IConvertible.ToInt16(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    int IConvertible.ToInt32(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    long IConvertible.ToInt64(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    sbyte IConvertible.ToSByte(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    float IConvertible.ToSingle(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    string IConvertible.ToString(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    object IConvertible.ToType(Type conversionType, IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    ushort IConvertible.ToUInt16(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    uint IConvertible.ToUInt32(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }

    ulong IConvertible.ToUInt64(IFormatProvider? provider)
    {
        throw new NotImplementedException();
    }
}

public class TLoggerConfig : IEntityTypeConfiguration<TLogger>
{
    public void Configure(EntityTypeBuilder<TLogger> builder)
    {
        builder.Property(ii => ii.ShortMsg).HasComputedColumnSql("SUBSTRING(msg,0,100)");
        builder.Property(ii => ii.Id).UseIdentityColumn(long.MaxValue, -1);
        builder.HasIndex(ii => new { ii.AppId, ii.Name });
        //builder.HasIndex(ii => new { ii.AppId, ii.Msg });
        builder.HasIndex(ii => ii.Dt);
    }
}

public class VLogger
{
    public long Id { get; set; }
    [Display(Name = "日期")]
    public string Dt { get; set; }
    [Display(Name = "类别")]
    public string Name { get; set; } = default!;
    [Display(Name = "等级")]
    public LogLevel LogLevel { get; set; } = default!;
    [Display(Name = "内容")]
    public string ShortMsg { get; set; } = default!;
    public VLogger(long id, DateTime dt, string name, int logLevel, string shortMsg)
    {
        Id = id;
        Dt = dt.ToString("MM-dd HH:mm:ss.fff");
        Name = name;
        LogLevel = (LogLevel)logLevel;
        ShortMsg = shortMsg;
    }
}
